using System;
using System.Collections;
using System.Data;
using Handrise.BasicModal.Serialization.ADOHelper;
using Handrise.BasicModal.Serialization.ISerial;


namespace Handrise.BasicModal.Serialization.Surrogate
{
    [Attributes.Overrider(typeof(System.Data.DataTable))]
    public class DataTableOverrider : Interfaces.IOverrider
    {
        public void Serialize(CompactFormatter parent, System.IO.Stream serializationStream, object graph)
        {
            DataTable dt = (DataTable)graph;
            byte[] barr = AdoNetHelper.SerializeDataTable(dt);
            byte[] bar_len_arr = BitConverter.GetBytes((Int32)barr.Length);
            serializationStream.Write(bar_len_arr, 0, bar_len_arr.Length);
            serializationStream.Write(barr, 0, barr.Length);
        }

        public object Deserialize(CompactFormatter parent, System.IO.Stream serializationStream)
        {
            byte[] barr_len = new byte[4];
            serializationStream.Read(barr_len, 0, 4);
            Int32 Len = BitConverter.ToInt32(barr_len, 0);
            byte[] barr = new byte[Len];
            serializationStream.Read(barr, 0, Len);
            return AdoNetHelper.DeserializeDataTable(barr);
        }
    }
}
